clear all
clear matrix
clear mata

set more off

local path = "...folder name"

cd "`path'"

******************************
* Link sinter - iron - steel * 
******************************

/*step 1: Merge stage-level dataset with tfp estimates then add facility identifier*/
use sinter_plant_pf.dta, clear

keep t firm plant time year month production size owner region labor grade_stability 

drop if production == .

gen tid = string(time)
gen fid = plant + tid

merge 1:1 fid using sinter_tfp_lmk_link.dta /*Add tfp estimates*/
drop _merge

merge m:1 plant using sinter_link.dta /*Add facility identifier*/
drop _merge

*trim outliers
drop if labor < 1 | labor > 10000
gen logtfp = log(tfp_sinter)
drop if logtfp > 5 

*adjust outliers of quality measure 
replace grade_stability = 0 if grade_stability < 0
replace grade_stability = 100 if grade_stability > 100

gen fid_sinter_iron = firm + tid + sinter_link
gen firm_sinter_iron = firm + sinter_link

/*step 2: aggregation across plants within stage according to the link*/
*create weights
bysort fid_sinter_iron: egen det_sinter = sum(w_det_sinter)
bysort fid_sinter_iron: egen pdt_sinter = sum(production)
bysort fid_sinter_iron: gen nb_sinter = _N

gen tfp1_sinter = log(tfp_sinter) 

replace tfp1_sinter = tfp1_sinter*w_det_sinter/det_sinter 
bysort fid_sinter_iron: egen tfp1 = sum(tfp1_sinter) /*weight tfp using deterministic component*/

drop tfp1_sinter 
rename tfp1 tfp1_sinter

*create weighted quality
replace grade_stability = grade_stability*production/pdt_sinter
bysort fid_sinter_iron: egen sinter_quality1 = sum(grade_stability)

bysort fid_sinter_iron: gen n = _n
keep if n == 1
drop n 

keep firm_sinter_iron fid_sinter_iron det_sinter pdt_sinter tfp1_sinter owner region sinter_quality1 nb_sinter
rename region region_sinter

save sinter_tfp_lmk_link_aggregate.dta, replace

/*repeat step 1 and step 2 for iron, have to merge with sinter as well*/
use iron_plant_pf.dta, clear

keep t firm plant time year month production size owner region labor grade1 

drop if production == .

gen tid = string(time)
gen fid = plant + tid

merge 1:1 fid using iron_tfp_lmk_link.dta /*Add tfp estimates*/
drop _merge

merge m:1 plant using iron_link.dta /*Add facility identifier*/
keep if _merge == 3
drop _merge

*trim outliers
drop if labor < 1
gen logtfp = log(tfp_iron)
drop if logtfp > 4 | logtfp < -4 

*iron quality outlier 
replace grade1 = 0 if grade1 < 0
replace grade1 = 100 if grade1 > 100


gen fid_sinter_iron = firm + tid + sinter_iron_link
gen fid_iron_steel = firm + tid + iron_steel_link

merge m:1 fid_sinter_iron using sinter_tfp_lmk_link_aggregate.dta /*merge with sinter productivity*/
drop _merge

/*step 2: aggregation across plants within stage according to the facility identifier*/
*create weights
bysort fid_iron_steel: egen det_iron = sum(w_det_iron)
bysort fid_iron_steel: egen pdt_iron = sum(production)
bysort fid_iron_steel: gen nb_iron = _N

gen tfp1_iron = log(tfp_iron) 
replace tfp1_iron = tfp1_iron*w_det_iron/det_iron 
bysort fid_iron_steel: egen tfp1 = sum(tfp1_iron) /*tfp by weight 1 using deterministic component*/

drop tfp1_iron 
rename tfp1 tfp1_iron

*weighted quality
replace grade1 = grade1*production/pdt_iron
bysort fid_iron_steel: egen iron_quality1 = sum(grade1)

*Ajust the weight for sintering plants according to according to fid_iron_steel
bysort fid_iron_steel fid_sinter_iron: gen n = _N
replace det_sinter = det_sinter/n
replace pdt_sinter = pdt_sinter/n
replace size_sinter = size_sinter/n
replace nb_sinter = nb_sinter/n
drop n

bysort fid_iron_steel: egen det_sinter_steel = sum(det_sinter)
bysort fid_iron_steel: egen pdt_sinter_steel = sum(pdt_sinter)
bysort fid_iron_steel: egen nb = sum(nb_sinter)
drop nb_sinter
rename nb nb_sinter 

********************************************************************************
replace tfp1_sinter = tfp1_sinter*det_sinter/det_sinter_steel
bysort fid_iron_steel: egen tfp1 = sum(tfp1_sinter)

drop tfp1_sinter 
rename tfp1 tfp1_sinter

*re-adjust sinter quality 
replace sinter_quality1 = sinter_quality1*pdt_sinter/pdt_sinter_steel
bysort fid_iron_steel: egen quality1 = sum(sinter_quality1)

drop sinter_quality1 
rename quality1 sinter_quality1

bysort fid_iron_steel: gen n = _n
keep if n == 1
drop n

keep owner firm firm_sinter_iron fid_sinter_iron fid_iron_steel det_sinter_steel pdt_sinter_steel det_iron size_iron pdt_iron tfp1* sinter_quality1 iron_quality1 nb* 

rename firm firm_add
rename owner owner_add

save sinter_iron_tfp_lmk_link_aggregate.dta, replace

/*repeat step 1 and step 2 for steel making and merge with sinter and iron*/

set more off

use steel_plant_pf.dta, clear
keep t firm plant time year month production size owner region tpigiron labor second 

drop if production == . 

gen tid = string(time)
gen fid = plant + tid

merge 1:1 fid using steel_tfp_lmk_link.dta /*Add steel tfp estimates*/
drop _merge

merge m:1 plant using steel_link.dta /*Add facility identifier*/
drop _merge

*trim outliers
drop if labor < 1 | labor > 500000 
gen logtfp = log(tfp_steel) 
drop if logtfp > 2 | logtfp <= -2 

*steel quality outlier 
replace second = 0 if second < 0
replace second = 100 if second > 100

gen fid_iron_steel = firm + tid + steel_link
gen firm_iron_steel = firm + steel_link

/*step 2: aggregation across plants within stage according to the link*/
*create weights 
bysort fid_iron_steel: egen det_steel = sum(w_det_steel)
bysort fid_iron_steel: egen pdt_steel = sum(production)
bysort fid_iron_steel: gen nb_steel = _N

gen tfp1_steel = log(tfp_steel) 

replace tfp1_steel = tfp1_steel*w_det_steel/det_steel
bysort fid_iron_steel: egen tfp1 = sum(tfp1_steel) /*tfp by weight 1 using deterministic component*/

drop tfp1_steel 
rename tfp1 tfp1_steel

*weighted quality
replace second = second*production/pdt_steel
bysort fid_iron_steel: egen steel_quality1 = sum(second)

bysort fid_iron_steel: gen n = _n
keep if n == 1
drop n 

merge 1:1 fid_iron_steel using sinter_iron_tfp_lmk_link_aggregate.dta /*Add information on sintering and pig-iron making*/
drop _merge

replace firm = firm_add if firm == ""
replace owner = owner_add if owner == ""

/*step 3: construct aggregate tfp using elasticities as weight*/
*method 1: using elasticity
gen gamma3 = 0.877800415 /*material elasticity for steel making*/
gen gamma2 = 0.436671586 /*material elasticity for pig-iron making*/
gen gamma23 = gamma2*gamma3
 
gen tfp1 = tfp1_steel + gamma3*tfp1_iron + gamma2*gamma3*tfp1_sinter

save 3stage_tfp_link_aggregate.dta, replace 

**************************************
*Generate estimates at facility level*
**************************************

use 3stage_tfp_link_aggregate.dta, clear


drop if plant == ""
drop if nb_sinter == 0 | nb_iron == 0 | nb_steel == 0
drop if nb_sinter == . | nb_iron == . | nb_steel == .

encode firm_iron_steel, gen(vi)
encode owner, gen(ownership) 
gen cluster1 = vi

xtset vi time, monthly

gen logsize = log(size_steel) 

/*generate estimates in Table 5 Column (1) and (2)*/
reg tfp1 i.ownership i.time 
outreg2 using vi_owner, keep(i.ownership) addtext(Time FE, YES) tex replace 

reg tfp1 i.ownership logsize i.time
outreg2 using vi_owner, keep(i.ownership logsize) addtext(Time FE, YES,Clustered by facility, YES) tex append

/*generate estimates in Table 8 Column (1) and (2) */
reg tfp1 i.ownership sinter_quality1 iron_quality1 steel_quality1 i.time
outreg2 using owner_quality, keep(i.ownership) addtext(sinter quality, YES, iron quality, YES, steel quality, YES, Time FE, YES) tex replace 

reg tfp1 i.ownership logsize sinter_quality1 iron_quality2 steel_quality1 i.time
outreg2 using owner_quality, keep(i.ownership logsize) addtext(sinter quality, YES, iron quality, YES, steel quality, YES, Time FE, YES) tex append





